const CloudDBZoneWrapper = require('./CloudDBZoneWrapper')

const myHandler = async function (event, context, callback, logger) {
  const cloudDBZoneWrapper = new CloudDBZoneWrapper(logger);
  const { action, payload } = JSON.parse(event.body || '{}')

  try {
    if (action === 'add') {
      // 转账金额小于等于0，直接取消
      if (payload.amount <= 0) {
        return callback({
          status: false,
          errMsg: '转账金额不能小于等于0'
        })
      }
      await cloudDBZoneWrapper.add(payload)
      return callback({
        status: true,
      });
    }

    if (action === 'query') {
      const data = await cloudDBZoneWrapper.query(payload);
      return callback({
        status: true,
        data
      })
    }

    if (action === 'queryTotal') {
      const data = await cloudDBZoneWrapper.queryTotal(payload);
      return callback({
        status: true,
        data
      })
    }


    callback({
      status: false,
      errMsg: 'no valid action'
    })
  } catch (e) {
    callback({
      status: false,
      errMsg: e.message
    })
  }
};

export { myHandler };